package com.thti.mecool.client.util;

import java.io.ByteArrayOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.TextPaint;
import android.widget.TextView;

/**
 * 
 * @author weiguo.ren
 * 
 */
public class StringUtil {
	private static String hexString = "1234567GHJKLMNBV";
	/**
	 * 字符串转换成十六进制值
	 * 
	 * @param bin
	 *            String 我们看到的要转换成十六进制的字符串
	 * @return
	 */
	public static String bin2hex(String bin) {
		char[] digital = "0123456789ABCDEF".toCharArray();
		StringBuffer sb = new StringBuffer("");
		byte[] bs = bin.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param hex
	 *            String 十六进制
	 * @return String 转换后的字符串
	 */
	public static String hex2bin(String hex) {
		String digital = "0123456789ABCDEF";
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return new String(bytes);
	}

	public static String toStringVersion(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
					.indexOf(bytes.charAt(i + 1))));

		return new String(baos.toByteArray());
	}

	public static boolean isBlank(String str) {
		int strLen;
		if ((str == null) || ((strLen = str.length()) == 0) || str.equals("null")) {
			return true;
		}
		for (int i = 0; i < strLen; ++i) {
			if (!(Character.isWhitespace(str.charAt(i)))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return (!(isBlank(str)));
	}

	public static String isString(String str) {
		if (str == null || str.equals("") || str.length()==0 || str.equals("null")) {
			return "";
		} else {
			return str;
		}
	}

	/**
	 * 截取字符串
	 * 
	 * @param s
	 *            要被截取的字符串
	 * @param iLen
	 *            截取的长度
	 * @param hasDot
	 *            是否加...
	 * @return
	 */
	public static String getSubString(String s, int iLen, boolean hasDot) {
		if (StringUtil.isNotBlank(s)) {
			char c = ' ';
			String sAsc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.+-";
			String lsResult = " ";
			int iGetedLen = 0;
			boolean flag = false;
			for (int i = 0; i < s.length(); i++) {
				c = s.charAt(i);
				if (sAsc.indexOf(c) >= 0) {
					lsResult += c;
					iGetedLen += 1;
				} else {
					lsResult += c;
					iGetedLen += 2;
				}
				if (iGetedLen >= iLen) {
					if (i + 1 < s.length()) {
						flag = true;
					}
					break;
				}
			}
			if (iGetedLen <= iLen) {
				if (flag) {
					if (hasDot) {
						return lsResult.trim() + "...";
					} else {
						return lsResult.trim();
					}
				} else {
					return lsResult.trim();
				}
			}
			if (hasDot) {
				lsResult = lsResult + "...";
			}
			return (lsResult.trim());
		} else {
			return "";
		}
	}
	
	/**
	 * 给控件文字设置 粗体
	 * @param view 需要设置 字体粗体 的控件
	 */
	public static void setFakeBoldText(TextView view){
		TextPaint tp = view.getPaint();
		tp.setFakeBoldText(true);
		
	}
	
	/**
	 * 判断字符串是否有特殊字符
	 */
	
	public static boolean isSpecialChar(String str){
		String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？_]";
		Pattern pattern=Pattern.compile(regEx);
		Matcher matcher=pattern.matcher(str);
		return matcher.find();
	}
	
}
